//
//  VM6ShareListVController.m
//  DoorBell
//
//  Created by Chun Hung Cheung on 2024/12/9.
//  Copyright © 2024 Chun Hung Cheung. All rights reserved.
//

#import "CatShareListViewController.h"
#import "CatShareListCell.h"
#import "CatShareCodeVController.h"
#import "CatEditShareUserVController.h"
@interface CatShareListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UILabel *share_list_label;
@property(nonatomic,strong)UIView *no_list_view;
@property(nonatomic,strong)UITableView *list_table_view;
@property(nonatomic,strong)UIButton *next_button;
@property(nonatomic,strong)NSMutableArray *share_userArray;


@end

@implementation CatShareListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.view.backgroundColor = [HexChangeRGB colorWithHexString:@"#F5F5F5"];
    
    [self setup];
    
    
    self.share_list_label.text = NSLocalizedString(@"分享列表",nil);
    
}


- (void)setup
{
     UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
     backbtn.frame = CGRectMake(0,0,40,80);
     backbtn.contentHorizontalAlignment =  UIControlContentHorizontalAlignmentLeft;
    
    [backbtn setImage:[UIImage imageNamed:@"cat_alarm_back"] forState:UIControlStateNormal];
    [backbtn addTarget:self action:@selector(selectLeftActionClick) forControlEvents:UIControlEventTouchUpInside];
    [backbtn SG_imagePositionStyle:SGImagePositionStyleDefault spacing:5];
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];
 
    UILabel *titleLabel  = [[UILabel alloc]init];
    titleLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
    titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:20.0];
    titleLabel.text = NSLocalizedString(@"设备分享", nil);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.frame = CGRectMake(0, 0, 80, 25);
    self.navigationItem.titleView = titleLabel;
}

- (void)selectLeftActionClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self get_share_list];
}



- (void)get_share_list
{
  
    YKIPinfo *ipinfo = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];
    
    [SVProgressHUD showWithDuration:5];
   
    __weak CatShareListViewController* weakself =  self;
    [AFNHTTPSRequest httpsRequestWithUrl:[YKBusinessFramework get_device_shared_user:self.bid] errorHint:^(id errorData) {
        [SVProgressHUD dismiss];
        NSLog(@"请求数据失败-->%@",errorData);
        if (weakself.list_table_view) {
           [weakself.list_table_view removeFromSuperview];
            weakself.list_table_view = nil;
        }
        self.no_list_view.backgroundColor = [UIColor clearColor];
        self.next_button.backgroundColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
    } result:^(id data) {
        [SVProgressHUD dismiss];
        NSDictionary *dict = (NSDictionary*)data;
        NSInteger code = [[dict objectForKey:@"code"] integerValue];
        if (code == 0) {
            self.share_userArray = [dict objectForKey:@"shared_users"];
            NSLog(@"%@",self.share_userArray);
            if (self.share_userArray.count  > 0) {
                
                if (weakself.no_list_view) {
                   [weakself.no_list_view removeFromSuperview];
                    weakself.no_list_view  = nil;
                }
                self.share_userArray = [dict objectForKey:@"shared_users"];
                
                [self.list_table_view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.view.mas_left).offset(14);
                    make.right.equalTo(self.view.mas_right).offset(-14);
                    make.top.equalTo(self.share_list_label.mas_bottom).offset(20);
                    make.height.mas_equalTo(50 + self.share_userArray.count*50);
                }];
                [self.list_table_view reloadData];
                 self.next_button.backgroundColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
            }else{
                if (weakself.list_table_view) {
                   [weakself.list_table_view removeFromSuperview];
                    weakself.list_table_view = nil;
                }
                self.no_list_view.backgroundColor = [UIColor clearColor];
                self.next_button.backgroundColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
            }
        }else{
            
             if (weakself.list_table_view) {
                [weakself.list_table_view removeFromSuperview];
                 weakself.list_table_view = nil;
            }
            self.no_list_view.backgroundColor = [UIColor clearColor];
            self.next_button.backgroundColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
        }
        
    }];
  
}


- (UILabel*)share_list_label
{
    if (!_share_list_label) {
         _share_list_label = [[UILabel alloc]init];
         _share_list_label.textColor = [HexChangeRGB colorWithHexString:@"#778CA2"];
         _share_list_label.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13.0];
         _share_list_label.numberOfLines = 0;
         _share_list_label.textAlignment = NSTextAlignmentLeft;
        [_share_list_label sizeToFit];
        [self.view addSubview:_share_list_label];
        [_share_list_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left).offset(12);
            make.top.equalTo(self.view.mas_top).offset([UIDevice dev_navigationFullHeight] + 20);
        }];
    }
    return _share_list_label;
}

- (NSMutableArray*)share_userArray
{
    if (!_share_userArray) {
         _share_userArray = [NSMutableArray array];
    }
    return _share_userArray;
}

- (UIView*)no_list_view
{
    if (!_no_list_view) {
         _no_list_view = [[UIView alloc]init];
        [_no_list_view sizeToFit];
        [self.view addSubview:_no_list_view];
        [_no_list_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view.mas_centerX);
            make.top.equalTo(self.share_list_label.mas_bottom).offset(180);
            make.height.mas_equalTo(200);
            make.width.mas_equalTo(220);
        }];
        
        
        UIImageView *image = [[UIImageView alloc]init];
        [_no_list_view addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_no_list_view.mas_top);
            make.left.equalTo(_no_list_view.mas_left);
            make.right.equalTo(_no_list_view.mas_right);
            make.height.mas_offset(150);
        }];
        image.image = [UIImage imageNamed:@"share_none"];
        
        UILabel *no_label = [[UILabel alloc]init];
        no_label.textColor = [HexChangeRGB colorWithHexString:@"#9B9B9B"];
        no_label.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18.0];
        no_label.numberOfLines = 0;
        no_label.textAlignment = NSTextAlignmentCenter;
       [no_label sizeToFit];
       [_no_list_view addSubview:no_label];
       [no_label mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.equalTo(_no_list_view.mas_centerX);
           make.left.equalTo(_no_list_view.mas_left).offset(14);
           make.right.equalTo(_no_list_view.mas_right).offset(-14);
           make.top.equalTo(image.mas_bottom).offset(30);
       }];
        no_label.text = NSLocalizedString(@"还没有共享给家人",nil);
        
    }
    return _no_list_view;
}





- (UITableView*)list_table_view
{
    if (!_list_table_view) {
          _list_table_view = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
          _list_table_view.backgroundColor  =[UIColor whiteColor];
          _list_table_view.layer.cornerRadius = 8;
          _list_table_view.layer.masksToBounds = YES;
        if (@available(iOS 11.0, *)) {
            _list_table_view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_list_table_view];
        [_list_table_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left).offset(14);
            make.right.equalTo(self.view.mas_right).offset(-14);
            make.top.equalTo(self.share_list_label.mas_bottom).offset(20);
            make.height.mas_equalTo(150 + self.share_userArray.count*50);
        
        }];
         _list_table_view.delegate   = self;
         _list_table_view.dataSource = self;
         _list_table_view.rowHeight  = 50;
         _list_table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
         _list_table_view.showsVerticalScrollIndicator = NO;
        [_list_table_view registerClass:[CatShareListCell class] forCellReuseIdentifier:@"CatShareListCell"];
    }

    return _list_table_view;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.share_userArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CatShareListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CatShareListCell" forIndexPath:indexPath];
    if (self.share_userArray.count) {
        NSDictionary *shareUserDict = self.share_userArray[indexPath.row];
        cell.dict = shareUserDict;
        
        if(indexPath.row == self.share_userArray.count-1){
            cell.lineview.hidden = YES;
        }
        
    }
   return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
     UIView *footer = [[UIView alloc]init];
     return footer;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
     CatEditShareUserVController *vc =[[CatEditShareUserVController alloc]init];
     vc.bid = self.bid;
     vc.share_dict = self.share_userArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    
}


-(UIButton*)next_button
{
    if (!_next_button) {
         _next_button = [[UIButton alloc]init];
         _next_button.layer.cornerRadius = 22;
         _next_button.layer.masksToBounds = YES;
        [_next_button addTarget:self action:@selector(nextButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_next_button];
        [_next_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(260);
            make.height.mas_equalTo(44);
            make.top.equalTo(self.view.mas_bottom).offset(-120);
            make.centerX.equalTo(self.view.mas_centerX);
        }];
        _next_button.backgroundColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
        _next_button.titleLabel.textColor = [UIColor whiteColor];
       [_next_button setTitle: NSLocalizedString(@"设备共享", nil) forState:UIControlStateNormal];
    }
    return _next_button;
}

- (void)nextButtonClick:(UIButton*)sender
{

    CatShareCodeVController *vc = [[CatShareCodeVController alloc]init];
    vc.bid = self.bid;
    [self.navigationController pushViewController:vc animated:YES];
}





@end
